<template>
  <el-dialog
    title=''
    size="large"
    width="460px"
    :modal="true"
    :close-on-click-modal="false"
    :destroy-on-close="true"
    :lock-scroll="false"
    :visible="visible"
    :before-close="handleClose">
    <div class="second_block">
      <div style="font-size: 20px;text-align: center;color: white;"><span v-if="buyerList.register_page_script">{{buyerList.register_page_script.script_1}}</span></div>
      <br>
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        label-width="100px">
        <el-form-item
          prop="name">
            <span slot="label">
              <span style="color: #000;font-weight: 700;">Name:</span>
            </span>
          <el-input
            :maxlength="20"
            v-model="form.name"
            placeholder="Enter Your Name..."
            clearable/>
        </el-form-item>
        <el-form-item
          v-if="buyerList.email_show ===1"
          prop="email">
            <span slot="label">
              <span style="color: #000;font-weight: 700;">Email:</span>
            </span>
          <el-input
            :maxlength="20"
            v-model="form.email"
            placeholder="Enter Your Email..."
            clearable/>
        </el-form-item>
        <el-form-item
          prop="order_id">
            <span slot="label">
              <span style="color: #000;font-weight: 700;">Order ID:</span>
            </span>
          <el-input
            :maxlength="20"
            v-model="form.order_id"
            placeholder="Enter Your Order ID..."
            clearable/>
        </el-form-item>
        <div v-if="emailDisplay && buyerList.activate_dispaly_email===1" style="text-align: center">
          <div style="color: #F56C6C">We can't find your order ID.</div>
          <div style="color: rgb(0, 0, 0)">If any question,please contact us.</div>
          <div style="color: rgb(0, 0, 0)">{{buyerList.contact_email}}</div>
        </div>

      </el-form>
    </div>
    <br>

    <div style="text-align: center">
      <span ><a href="https://bnbapo.com/Index/Index/findid.html" target="_blank" style="color: rgb(254,137,4)">How to find your Order ID?</a></span>
    </div>
    <br>
    <div style="text-align: center">
      <el-button type="warning" @click="doNext">Activate</el-button>
    </div>

  </el-dialog>
</template>

<script>
import NProgress from "nprogress";

export default {
  name: "register",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    data: Object,
    buyerData: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}, this.data),
      buyerList: Object.assign({}, this.buyerData),
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      //是否显示联系邮箱
      emailDisplay:false,
      record_id:0,
    };
  },
  computed: {
    // 表单验证规则
    rules() {
      return {
        name: [
          {required: true, message: 'Enter Your Name...', type: 'string', trigger: 'blur'}
        ],
        email: [
          {required: true, message: 'Enter Your Email...', type: 'string', trigger: 'blur'}
        ],
        order_id: [
          {required: true, message: 'Enter Your Order ID...', type: 'string', trigger: 'blur'}
        ]
      };
    },
    // 当前语言
    language() {
      return this.$i18n.locale;
    }
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
      } else {
        this.form = {};
      }
    },
  },
  methods : {
    handleClose(done) {
      done();
      this.$emit('handleClose');
    },
    setBuyerConfig(data){
      this.buyerList = data;
    },
    setRecordId(record_id)
    {
      this.record_id = record_id;
    },
    //跳转礼品页
    doNext()
    {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return false;
        }else{
          this.loading = true;
          this.form.record_id = this.record_id;
          this.$http.post('/register', this.form).then((res) => {
            this.loading = false;
            if (res.data.code === 0) {
              var registerData = res.data.data;
              this.form.asin = registerData.asin;
              this.form.display_comment_link_view = registerData.display_comment_link_view;
              this.form.display_gift_select_view = registerData.display_gift_select_view;
              this.form.product_title = registerData.product_title;
              this.form.platform_sku = registerData.platform_sku;
              this.form.brand_code = registerData.brand_code;
              this.form.gift_info = registerData.gift_info;
              this.form.phone = registerData.order_phone;
              if (this.buyerList.address_default_clear == 2){
                this.form.address = registerData.order_address;
              }
              this.form.state = registerData.order_state;
              this.form.postal_code = registerData.order_postal_code;
              this.form.country = registerData.order_country_code;
              this.form.city = registerData.order_city_name;
              // this.$message.success('激活成功');
              this.$message({type:'success',message:res.data.msg,offset:160});
              setTimeout(() => {
                NProgress.done(true);
                if (registerData.progress_activate !==1){
                  this.$emit('doActivateEnd');
                }else{
                  this.$emit('doNext',this.form);
                }

              }, 1500);
            } else {
              // this.$message.error(res.data.msg);
              this.$message({type:'error',message:res.data.msg,offset:160});
              if (res.data.msg.includes('订单不存在')){
                this.emailDisplay = true;
              }
            }
          });

        }
      });


    },
  }
}
</script>

<style scoped>
::v-deep .el-dialog__header {
  border-bottom: 0px solid var(--border-color-lighter);
}
.el-dialog{
  border-radius: 15px 15px 8px 8px;
}
</style>
